package com.eralink.uhfreaderlib;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

final class ConnectConfigParser {
    private static final Set<Integer> bauds = new HashSet<Integer>() {
        { add(4800); add(9600); add(19200); add(38400); add(57600); add(115200); add(230400);}
    };
    private final HashMap<String, String> fields = new HashMap<>();

    public ConnectConfigParser() {

    }

    public boolean parse(String connect_str) {
        for (String section : connect_str.trim().split(";")) {
            final String field = section.trim();
            final String[] pair = field.split("=");
            if (2 != pair.length) {
                return false;
            }

            fields.put(pair[0].trim(), pair[1].trim());
        }

        return true;
    }

    public boolean getValueByName(String name, final String[] value) {
        if (!fields.containsKey(name)) {
            return false;
        }

        value[0] = fields.get(name);

        return true;
    }

    public boolean validate() {
        if (!fields.containsKey("CommType")) {
            return false;
        }

        if (Objects.equals(fields.get("CommType"), "COM")) {
            return validateForCom();
        }
        else if (Objects.equals(fields.get("CommType"), "NET")) {
            return validateForNet();
        }

        return false;
    }

    private boolean validateForCom() {
        if (!fields.containsKey("CommType") || !Objects.equals(fields.get("CommType"), "COM")) {
            return false;
        }

        if (!fields.containsKey("ComPath") || fields.get("ComPath").isEmpty()) {
            return false;
        }

        final int[] value = new int[1];
        if (fields.containsKey("Address") &&
                (!Utils.parseInteger(fields.get("Address"), value) || 0 > value[0] || 255 < value[0])
            ) {
            return false;
        }

        // 默认115200, 合法值: [4800, 9600, 19200, 38400, 57600, 115200, 230400]
        if (fields.containsKey("Baud") &&
                (!Utils.parseInteger(fields.get("Baud"), value) || !bauds.contains(value[0]))
            ) {
            return false;
        }

        // 默认8,可选值为5~8
        if (fields.containsKey("Databits") &&
                (!Utils.parseInteger(fields.get("Databits"), value) || 5 > value[0] || 8 < value[0])
            ) {
            return false;
        }

        // 默认1；1:1位停止位；2:2位停止位
        if (fields.containsKey("Stopbits") &&
                (!Utils.parseInteger(fields.get("Stopbits"), value) || 1 > value[0] || 2 < value[0])
            ) {
            return false;
        }

        // 0:无校验位(NONE，默认)；1:奇校验位(ODD);2:偶校验位(EVEN)
        if (fields.containsKey("Parity") &&
                (!Utils.parseInteger(fields.get("Parity"), value) || 0 > value[0] || 2 < value[0])
            ) {
            return false;
        }

        return true;
    }

    private boolean validateForNet() {
        if (!fields.containsKey("CommType") || !fields.get("CommType").equals("NET")) {
            return false;
        }

        if (!fields.containsKey("RemoteIp") || !Utils.ipCheck(fields.get("RemoteIp"))) {
            return false;
        }

        final int[] value = new int[1];
        if (!fields.containsKey("RemotePort") || !Utils.parseInteger(fields.get("RemotePort"), value) ||
                0 > value[0] || 65535 < value[0]) {
            return false;
        }

        return true;
    }
}
